home *** CD-ROM | disk | FTP | other *** search
- Path: news.lpr.carel.fi!usenet
- From: Ari Lukumies <aril@cmt.lpr.mail.carel.fi>
- Newsgroups: comp.lang.c
- Subject: Re: revised code of calling a function twice from printf
- Date: Tue, 05 Mar 1996 13:36:36 +0200
- Organization: Carelcomp Forest
- Message-ID: <313C2744.7FE7@cmt.lpr.mail.carel.fi>
- References: <4hfs54$k4e@newsbf02.news.aol.com>
- NNTP-Posting-Host: renoir.cclahti.carel.fi
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Razine wrote:
- >
- > Here is the actual code , modified with everyone's suggestions. However
- > there is still a bug in here somewhere. Can anyone please explain what is
- > wrong with this.
- >
- > It returns
- >
- > [1] NONE [2] NE
- >
- > I would like it to return
- >
- > [1] NONE [2] Asprin
- >
- > Thank you very much for your help. it is greatly appreciated.
- >
- > #include <stdio.h>
- > #include <string.h>
- >
- > char *display_drug_type(int drug_index);
- >
- > int drug_inventory[5]={0,1,0,0,0};
- >
- > int main() {
- >
- > printf("[1] %s [2] %s
- > \n",display_drug_type(0),display_drug_type(1));
- >
- > return 0;
- > }
- >
- > char *display_drug_type(int drug_index) {
- > char drug_type[81]="\0";
- >
- > switch(drug_inventory[drug_index]) {
- > case 0 : strcpy(drug_type,"NONE"); break;
- > case 1 : strcpy(drug_type,"Asprin"); break;
- > default : printf("Error in Display_drug_inventory");
- > }
- > return drug_type;
- > }
-
- In the printf, you call the same function twice. The order in which it gets called is
- undetermined, thus the contents of drug_type will be undetermined. Why not rewrite
- display_drug_type as:
-
- char *display_drug_type(int drug_index)
- {
- switch (drug_inventory[drug_index]) {
- case 0: return "NONE";
- case 1: return "Asprin";
- }
- return "Invalid";
- }
-
- Later,
- AriL
- --
- All my opinions are mine and mine alone.
-